package com.junxonline.instant.job.handler;

import com.junxonline.instant.common.constant.MinioBucketConstant;
import com.junxonline.instant.common.util.job.MinioUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 清理Minio任务
 *
 * @author JunX
 * @date 2022-05-27
 */
@Log4j2
@Component
public class ClearMinioTrashJob {

    @Autowired
    private MinioUtils minioUtils;

    @XxlJob("clearMinioTrashJob")
    public ReturnT<String> clearMinioTrashJob() {
        log.info("-------开始执行[清理Minio任务]-------");
        try {
            clearMinioTrash();
        } catch (Exception e) {
            log.error("-------[清理Minio任务]执行出错-------");
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    private void clearMinioTrash() throws Exception {
        // 清理无用的截屏文件
        clearScreenshotTrash();
        log.info("-------[清理Minio任务]执行成功-------");
    }

    private void clearScreenshotTrash() {
        minioUtils.removeBucketAllObject(MinioBucketConstant.MINIO_BUCKET_SCREENSHOT);
    }

}
